package com.superhelper.superhelper_web.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.superhelper.superhelper_web.domain.PageModel;
import com.superhelper.superhelper_web.domain.Resource;
import com.superhelper.superhelper_web.domain.RoleResource;

/**
 * ResourceMapper
 * 
 * @author Administrator
 *
 */

public interface ResourceMapper {

	/**
	 * 添加管理员权限资源
	 * 
	 * @return
	 */
	@Insert("insert into role_resource(roleId,resourceId)values(#{a.roleId},#{a.resourceId})")
	int addAdminResource(@Param("a") RoleResource ur);

	/**
	 * 添加管理员权限资源(批量)
	 * 
	 * @return
	 */
	int saveAdminResource(List<RoleResource> resourceIds);

	/**
	 * 删除管理员权限资源
	 */
	@Delete("delete from role_resource where roleId=#{roleId}")
	int delAdminResourceByRoleId(@Param("roleId") Integer roleId);

	/**
	 * 获取管理员权限资源
	 */
	@Select("select * from role_resource where roleId=#{roleId}")
	List<RoleResource> getResourceByRoleId(@Param("roleId") Integer roleId);

	/**
	 * 获取管理员权限资源
	 */
	@Select("select * from resource order by id")
	List<Resource> getAllResource();

	/**
	 * 资源列表
	 * 
	 * @return
	 */
	@Select("select * from resource order by id desc limit #{page.startRow},#{page.pageSize} ")
	List<Resource> getResource(@Param("page") PageModel pageModel);

	@Select("select count(1) from resource")
	int countResource();

	/**
	 * 资源详情
	 * 
	 * @return
	 */
	@Select("select * from resource where id=#{id}")
	Resource getResourceById(@Param("id") Integer id);

	/**
	 * 获取用户对应的视图资源
	 * 
	 * @param userId
	 * @return
	 */
	@Select("SELECT r.* FROM resource r,role_resource rr WHERE rr.resourceId=r.id and rr.roleId IN(${ids}) order by r.parentId asc ,r.indexs asc ")
	List<Resource> getModelListByRoleId(@Param("ids") String ids);

	/**
	 * 获取用户对应的视图资源
	 * 
	 * @param userId
	 * @return
	 */
	@Select("SELECT * FROM resource WHERE  parentId=#{pid} order by r.parentId asc ,r.indexs asc ")
	List<Resource> getSuperAdminListById(@Param("pid") Integer pid);

	@Insert("insert into resource(name,url,parentId)values(#{entity.name},#{entity.url},#{entity.parentId})")
	Integer saveResource(@Param("entity") Resource entity);

	@Update("update resource set name=#{entity.name},url=#{entity.url},parentId=#{entity.parentId} where id=#{entity.id}")
	Integer updateResource(@Param("entity") Resource entity);

	@Delete("delete from resource where id=#{resourceId}")
	Integer deleteResource(@Param("resourceId") Long resourceId);

	/**
	 * 删除管理员权限资源
	 */
	@Delete("delete from role_resource where resourceId=#{resourceId}")
	int delRelationByResourceId(@Param("resourceId") Long resourceId);

}
